# -*- mode: Awk; -*-  vim: set filetype=awk : 
#
# This file is part of KNIT; copyright (C) 2010 by Tim Menzies
# tim@menzies.us.
#
# KNIT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# KNIT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with KNIT.  If not, see <http://www.gnu.org/licenses/>.

function slotsPlugIns(str,slots,   tmp) {
    split(str,tmp,";")
    if (tmp[1]=="date")      return plugInDate(     str,slots)
    if (tmp[1]=="shell")     return plugInShell(    str,slots)
    if (tmp[1]=="quotes")    return plugInQuotes(   str,slots)
    if (tmp[1]=="testSuite") return plugInTestSuite(str,slots)
    return str
}
function plugInQuotes(str,slots,    n,tmp) {
    srand(systime() + PROCINFO["pid"])
    n=split(slots["quotes"],tmp,"\n")
    return tmp[int(rand()*n) + 1]
}
function plugInTestSuite(str,slots,   out,com,line) {
	com="cd src; make -s score"
	while ((com | getline line) > 0)
			out = out "\n" line
	close(com)
	gsub(/(PASSED|FAILED)/,"<li>&",out)
	gsub(/FAILED/,"<font color=red>FAILED</font>",out)
	return "<ol>"out"</ol>"
}
function plugInDate(str,slots) {
	 return strftime("%A") " "\
	        strftime("%B") " "\
	        strftime("%d") ", "\
	        strftime("%I") ":"\
	        strftime("%M") \
	        tolower(strftime("%p")) \
}
function plugInShell(str,slots,   tmp,i,com,out1,out) {
	n=split(str,tmp,";")
	for(i=2;i<=n;i++) {
		com=tmp[i]
		com | getline out1
		close(com)
		out = out out1
	}
	return out
}
